//
//  LoginViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/8.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON

struct LoginModel: HandyJSON {
    var token: String?
    var userId: Int?
    var easyPassword: String?
    var avatar: String?
    /// 头像高清图
    var avatarUrl: String?
    var name: String?
    var sex: String?
    /// nemosdk account
    var videoAccountDTO: LoginVideoAccountDTOModel?
}

struct LoginVideoAccountDTOModel: HandyJSON {
    var password: String?
    var account: String?
}


struct LoginViewModel {
    let model: LoginModel
    
    var token: String { return model.token ?? "" }
    
    var userId: Int { return model.userId ?? 0 }
    
    var name: String { return model.name ?? "" }
    
    var sex: String { return model.sex ?? "" }
}

extension LoginViewModel {
    func save(account: String, password: String) {
        UserDefaults.saveToken(token)
        UserDefaults.saveUserId(userId)
        UserDefaults.saveUsername(name)
        UserDefaults.saveUserSex(sex)
        UserDefaults.saveUserAvatar(model.avatar ?? "")
        UserDefaults.saveUserAvatarHigh(model.avatarUrl ?? "")
        UserDefaults.saveVideoAccountDTO(model.videoAccountDTO)
        if UserDefaults.isRemember {
            UserDefaults.saveAccount(account)
            UserDefaults.savePassword(password)
        }
    }
}

extension LoginViewModel {
    static func login(account: String,
                      password: String,
                      success: @escaping (_ model: LoginModel) -> Void,
                      failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = [
            "userName": account,
            "password": password
        ]
        YSGLog("登录 paras：\(paras)")
        HttpTool.shared.request(target: ApiManager.login(paras: paras), success: { (json, code) in
            YSGLog("登录 json：\(json)")
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let model = JSONDeserializer<LoginModel>.deserializeFrom(json: json["data"].description) {
                    success(model)
                } else {
                    failure("登录失败，请重试!")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
    
    /// 绑定推送设备
    /// 0 -安卓 1 - ios
    static func bindDeviceTokenForPush(deviceToken: String,
                                       deviceType: Int = 1,
                                       finished: @escaping (_ errorMsg: String?) -> Void)
    {
        let paras: [String: Any] = ["mobileNum": deviceToken, "mobileType": deviceType]
        HttpTool.shared.request(target: ApiManager.bindDeviceTokenForPush(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                finished(json["message"].description)
            case .success:
                finished(nil)
            }
        }) { errorMsg in
            finished(errorMsg)
        }
    }
}
